import axios from "axios";
import { Message } from "element-ui";

// 对 axios 进行二次封装
const service = axios.create({
  baseURL:'http://127.0.0.1:3000',
  timeout:5000,
})

// 响应拦截器
service.interceptors.response.use(
  response => {
    let res = response.data

    if(res.code === 201){
      Message.success(res.msg)

      return res
    }else if(res.code === 200){
      return res
    }else{
      Message.error(res.msg)
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)
export default service